<script>
import userApi from "@/api/user_api.js";

export default {
  name: "Register",
  data() {
    //新密码与重复密码不一致，一定写在data里但不是return里
    let repeatValidate = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        account: "",
        password: "",
        checkPassword: "",
        name: "",
        phone: ""
      },
      rules: {
        account: [
          {required: true, message: '账号名不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '新密码不能为空', trigger: 'blur'},
          {min: 4, max: 16, message: '长度应在 4 到 16 个字符', trigger: 'blur'}
        ],
        checkPassword: [
          {required: true, message: '请再输入一遍新密码', trigger: 'blur'},
          {validator: repeatValidate, trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 2, max: 10, message: '姓名长度在 2 到 10 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    register() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          userApi.register(this.form).then(response => {
            const loginForm = {
              account: this.form.account,
              password: this.form.password
            }
            userApi.login(loginForm).then(response => {
              this.$router.push('/home')
            })
          })
        }
      })
    }
  }
}
</script>

<template>
  <div class="v-center">
    <h1 style="color: #2c3e50">用户注册</h1>
    <el-form :model="form" ref="registerForm" :rules="rules"
             label-width="auto" style="max-width: 50vh; margin:0 auto;" size="large">
      <el-form-item label="用户名" prop="account">
        <el-input v-model="form.account" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" show-password v-model.trim="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input type="password" show-password v-model.trim="form.checkPassword"
                  placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="form.phone" placeholder="请填写手机号码"></el-input>
      </el-form-item>
      <el-form-item label="">
        <div class="buttonClass">
          <el-button type="primary" style="width: 40vh" @click="register">注册并登录</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>


</template>

<style scoped>
.buttonClass {
  margin: auto;
}

.v-center {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  height: 80vh;
}
</style>